SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


alter    trigger tb_comexpproportionexpense_au on dbo.tb_comexpproportionexpense for update 
as
begin
  set nocount on
  declare @nbid int, 
          @nbno int,
          @nrelationcount int,
          @nrelationbtype int, 
          @nrelationbid int, 
          @namount decimal(18,2), 
          @ndcamount decimal(18,2), 
          @obid int, 
          @obno int,
          @orelationcount int,
          @orelationtype int, 
          @orelationbid int, 
          @oamount decimal(18,2), 
          @odcamount decimal(18,2)
  declare tb_comexpproportionexpense_cur cursor local forward_only static optimistic for 
  select n.bid,n.bno,n.relationcount,n.relationbtype,n.relationbid,n.dcamount,n.amount,
  o.bid,o.bno,o.relationcount,o.relationbtype,o.relationbid,o.dcamount,o.amount
  from inserted n,deleted o where n.bid = o.bid
  open tb_comexpproportionexpense_cur
  
  fetch next from tb_comexpproportionexpense_cur into 
  @nbid, @nbno, @nrelationcount, @nrelationbtype, @nrelationbid, @ndcamount,@namount,
  @obid, @obno, @orelationcount, @orelationtype, @orelationbid, @odcamount,@oamount
  while (@@fetch_status = 0)
  begin
    update tb_comexpproportion set amount=amount-@odcamount+@ndcamount where bid=@nbid

    if (@nrelationbid<>@orelationbid or @odcamount <> @ndcamount)
    begin
      update tb_ComExpenditure set dcappamt=dcappamt-@odcamount+@ndcamount, appamt=isnull(appamt,0)-isnull(@oamount,0)+isnull(@namount,0)  
      where bid=@orelationbid
    end

    fetch next from tb_comexpproportionexpense_cur into 
    @nbid, @nbno, @nrelationcount, @nrelationbtype, @nrelationbid, @ndcamount,@namount,
    @obid, @obno, @orelationcount, @orelationtype, @orelationbid, @odcamount,@oamount   
  end
  close tb_comexpproportionexpense_cur
  deallocate tb_comexpproportionexpense_cur
end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

